

/** 加载页 */

// import { RinzzConfig } from "../RinzzWrapper/Common/RinzzConfig";
import GameData_blxf from "./GameData/GameData";
import { BundleManager_blxf } from "./Manager/BundleManager";
import { CsvManager_blxf } from "./Manager/CsvManager";
import { EnumManager_blxf } from "./Manager/EnumManager";
import { PlatformManager_blxf } from "./Manager/PlatformManager";
import { ConfigModule_blxf } from "./Module/ConfigModule";
import { LoadModule_blxf } from "./Module/LoadModule";
import NodePool_blxf from "./Module/NodePool";
import { UtilsModule_blxf } from "./Module/UtilsModule";
// import GameTipsPanel_blxf from "./Panel/gameTipsPanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Loading_blxf extends cc.Component {

    @property({ tooltip: '游戏场景名' })
    _gameSceneName: string = 'Game';

    @property({ tooltip: '资源已加载进度' })
    _bundleLoadRatio: number = 0;

    @property({ tooltip: '预制体预加载进度' })
    _prefabLoadRatio: number = 0;

    @property({ tooltip: '显示已加载进度' })
    _nowLoadRatio: number = 0;

    @property({ tooltip: 'csv配置表加载进度' })
    _csvLoadRatio: number = 0;

    @property({ tooltip: '是否已加载游戏场景' })
    _isLoadScene: boolean = false;

    @property({ tooltip: '顶部文字' })
    _topText: string = "BGM by 夕阳对面的猫 & biubiubiu_713 & Shona-_2";

    @property({ tooltip: '适龄提示面板' })
    _gameTipsPanel: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

  async  onLoad () {
        ConfigModule_blxf.setGameName_blxf();

        this.onLanguage_blxf();
        
        // 隐藏网页DEBUG信息
        cc.debug.setDisplayStats( false );

        this.initEvent_blxf();
        
        this._bundleLoadRatio = 0;
        this._prefabLoadRatio = 0;
        this._csvLoadRatio = 0;
        this._nowLoadRatio = 0;

        PlatformManager_blxf.instance.initManager_blxf(()=>{
            
        });
    }

    start () {
        this.updateUI_blxf();

        this.loadGameBundle_blxf();

        this.onPreloadScene_blxf();
    }

    update ( dt: number ) {
        let _loadRatio = this._bundleLoadRatio * 0.6 + this._prefabLoadRatio * 0.3 + this._csvLoadRatio * 0.1;
        let _isCheck: boolean = this._nowLoadRatio >= _loadRatio && this._nowLoadRatio != 1;
        !_isCheck && this.updateLoadProgress_blxf( dt );
    }

    updateUI_blxf () {

    }

    // 龍萬騰運興馬祿躍雙發財達財廣
    
    /** 更新加载进度 */
    updateLoadProgress_blxf ( dt: number = 0 ) {
        if ( this._isLoadScene ) return;

        this._nowLoadRatio += 0.0167 * 1;
        if ( this._nowLoadRatio > 1 ) this._nowLoadRatio = 1;
        let _ratioText: string = `${Math.floor(this._nowLoadRatio * 100)}%`;

        // 检测是否已加载完成
        let _isLoadOk: boolean = this._nowLoadRatio >= 1;
        if ( !_isLoadOk ) return;

        if ( this._gameTipsPanel ) return;
        if ( !this.onCheckPrivacy() ) return;

        this.onLoadScene_blxf();

        // 确认当前宿主版本是否支持跳转某个小游戏入口场景
        PlatformManager_blxf.instance.onCheckScene_blxf();
    }

    /** 加载Bundle */
    loadGameBundle_blxf () {
        let _gameBundleType = EnumManager_blxf.instance.getGameBundleType();
        let _callback: object = ( loadRatio: number, bundleName: string ) =>{
            this._bundleLoadRatio = loadRatio;

            let _isCsvBundle: boolean = bundleName == _gameBundleType.BUNDLE_CSV;
            _isCsvBundle && CsvManager_blxf.instance.loadGameCsv_blxf(( csvRatio: number )=>{
                this._csvLoadRatio = csvRatio;
            });
            // 预加载预制体
            let _isPfBundle: boolean = bundleName == _gameBundleType.BUNDLE_PREFAB;
            _isPfBundle && this.onPreloadPrefab();
        };
        BundleManager_blxf.instance.loadGameBundle_blxf( _callback, 0 );
    }

    /** 预加载预制体 */
    onPreloadPrefab () {
        let {
            PREFAB_GAME_BTN, PREFAB_VENDOR_PANEL, PREFAB_GUIDANCE,
            PREFAB_SHOP_ITEM, PREFAB_GOLD_ITEM, PREFAB_DIAMOND_ITEM, PREFAB_ICON_NUM_ITEM
        } = EnumManager_blxf.prefabNameType;
        let _pfNames: string[] = [ 
            // PREFAB_GAME_BTN, PREFAB_VENDOR_PANEL, 
            PREFAB_GUIDANCE,
            PREFAB_SHOP_ITEM, PREFAB_GOLD_ITEM, PREFAB_DIAMOND_ITEM, PREFAB_ICON_NUM_ITEM
        ];

        LoadModule_blxf.instance.LoadPrefab_blxf( _pfNames, ( ok: number, total: number )=>{
            this._prefabLoadRatio = ok / total;
        }, ()=>{
            NodePool_blxf.autoLoad( [ PREFAB_SHOP_ITEM, PREFAB_GOLD_ITEM, PREFAB_DIAMOND_ITEM, PREFAB_ICON_NUM_ITEM ], [ 5 * 12, 10 * 12, 10 * 10, 12 ] );
        } );
    }

    /** 预加载游戏场景 */
    onPreloadScene_blxf () {
        // cc.director.preloadScene( this._gameSceneName );
    }

    /** 加载游戏场景 */
    async onLoadScene_blxf () {
        this._isLoadScene = true;

        await this.loadGameData_blxf();


        UtilsModule_blxf.onReportEvent_blxf( 1, 1 );
    };

    loadScene_blxf () {
        // cc.director.loadScene( this._gameSceneName );
        cc.game.emit('loadGame_blxf');
    }

    /** 加载游戏数据 */
    async loadGameData_blxf () {
        await GameData_blxf.instance.loadGameData_blxf(()=>{
            this.loadScene_blxf();
        });

        // // todo test method
        // GameData_blxf.instance.setPassNum_test_blxf();

        // // 兼容
        // UtilsModule_blxf.instance.onCheckUnlockShopId_blxf();
        // UtilsModule_blxf.instance.onCheckUnlockDecorationGuest_blxf();
    }


    /**
     * 获取游戏图标类型
     * @returns _logoType: 游戏logo类型（1：80小贩、2：致富高手 ）
     */
    getLogoType_blxf () {
        let _logoType: number = 1;

        let { gameType } = ConfigModule_blxf;
        switch ( gameType ) {
            case 1 : _logoType = 1; break;
            case 4 : _logoType = 4; break;
            case 5 : _logoType = 5; break;
        }

        let { PLATFORM_TT } = EnumManager_blxf.platformType;
        let _platformName: string = ConfigModule_blxf.getPlatformName_blxf();
        switch ( _platformName ) {
            case PLATFORM_TT : _logoType = 2; break;
            // default : _logoType = 1; break;
        }

        return _logoType;
    }

    initEvent_blxf () {
        cc.game.once(cc.game.EVENT_ENGINE_INITED, ()=>{
            let oldHandleLoadedTexture = cc.Texture2D.prototype.handleLoadedTexture;
            let optimizedHandleLoadedTexture = function ( premultiplied: any ) {
                oldHandleLoadedTexture.call(this, premultiplied);
            }
            cc.Texture2D.prototype.handleLoadedTexture = optimizedHandleLoadedTexture;
        })
    }

    /** 检测隐私政策 */
    onCheckPrivacy () {
        let isCheck = true;
        let _isShowPrivacyValue = cc.sys.localStorage.getItem('isShowPrivacyValue');
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME :
            case cc.sys.DESKTOP_BROWSER : isCheck = true; break;
            // default : isCheck = !RinzzConfig.isPrivacy || _isShowPrivacyValue; break;
        }
        return isCheck;
    }

    /** 点击适龄提示 */
    onTouchTips () {
        // this._gameTipsPanel = LoadModule_blxf.instance.LoadNode_blxf({
        //     prefabNode: this.gameTipsPanel, parentNode: this.node
        // })
        // let _this = this._gameTipsPanel.getComponent(GameTipsPanel_blxf);

        // let _options = {
        //     closeCB: () =>{
        //         this._gameTipsPanel = null;
        //     }
        // }
        // _this && _this.initData_blxf && _this.initData_blxf( _options );
    }
    
    /** 检测语言类型 */
    onLanguage_blxf () {
        let _isHant = UtilsModule_blxf.instance.isTraditionalChinese_blxf();
        ConfigModule_blxf.instance.setLanguage_blxf( _isHant ? 2 : 1 );
    }

}
